package com.miao.hong.fang.user.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.jdbc.core.RowCallbackHandler;

import com.miao.hong.fang.admin.ContactInfoTO;
import com.miao.hong.fang.base.dao.BaseDAO;
import com.miao.hong.fang.user.UserAccessInfoTO;
import com.miao.hong.fang.user.UserSubscribeCourseTO;
import com.miao.hong.fang.user.UserTO;

public class UserDAOImpl extends BaseDAO implements UserDAO{

	public boolean checkUserId(String loginName) {
		List list = getHibernateTemplate().find("from UserTO where loginName = ?", loginName);
		return list.isEmpty() ? true : false;
	}
	public void registerUser(UserTO user) {
		getHibernateTemplate().save(user);
	}
	
	public List getUserSubscribed(String userId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(UserSubscribeCourseTO.class);
		criteria.add(Restrictions.eq("userId", userId));
		
		return getHibernateTemplate().findByCriteria(criteria);
	}
	
	public UserTO getUserByLoginName(String loginName) {
		List list = getHibernateTemplate().find("from UserTO where loginName = ?", loginName);
		if(!list.isEmpty()) {
			return (UserTO) list.get(0);
		}
		return null;
	}
	
	public UserAccessInfoTO getAccessInfo(String sessionId) {
		return (UserAccessInfoTO) getHibernateTemplate().get(UserAccessInfoTO.class, sessionId);
	}
	
	public void logAccessInfo(UserAccessInfoTO to) {
		getHibernateTemplate().save(to);
	}
	
	public void updateAccessInfo(UserAccessInfoTO to) {
		getHibernateTemplate().update(to);
	}
	
	public void subscribeCourse(UserSubscribeCourseTO to) {
		getHibernateTemplate().save(to);
	}
	
	public UserSubscribeCourseTO checkUserCourse(UserSubscribeCourseTO to) {
		return (UserSubscribeCourseTO) getHibernateTemplate().get(UserSubscribeCourseTO.class, to);
	}
	
	public void cancelSubscribeCourse(String userId, String courseId) {
		UserSubscribeCourseTO to = new UserSubscribeCourseTO();
		to.setUserId(userId);
		to.setCourseId(courseId);
		to.setVersion(0);
		
		getHibernateTemplate().delete(to);
	}
	
	public void addContactInfo(String aboutUs, String contantPerson, String phone, String email, String qq, String msn)  {
	
		StringBuffer sql = new StringBuffer();
		sql.append("insert into TBL_CONTACT_INFO values ( '1'");
		sql.append(", '").append(aboutUs).append("'");
		sql.append(", '").append(contantPerson).append("'");
		sql.append(", '").append(phone).append("'");
		sql.append(", '").append(email).append("'");
		sql.append(", '").append(qq).append("'");
		sql.append(", '").append(msn).append("'");
		sql.append(");");
		
		getJdbcTemplate().execute(sql.toString());
	}
	
	public ContactInfoTO getContactInfo() {
		final ContactInfoTO to = new ContactInfoTO();
		
		getJdbcTemplate().query("select * from TBL_CONTACT_INFO where id = '1'", 
				new RowCallbackHandler(){
					public void processRow(ResultSet rs) throws SQLException {
						to.setAboutUs(rs.getString(2));
						to.setContactPerson(rs.getString(3));
						to.setPhone(rs.getString(4));
						to.setEmail(rs.getString(5));
						to.setQq(rs.getString(6));
						to.setMsn(rs.getString(7));
					}
		});
		
		return to;
	}
	
	public void updateContactInfo(ContactInfoTO to) {
		getJdbcTemplate().execute("delete from tbl_contact_info where id = '1'");
		addContactInfo(to.getAboutUs(), to.getContactPerson(), to.getPhone(), to.getEmail(), to.getQq(), to.getMsn());
	}
	
}
